package com.syqc.monitor.ui.cost

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.entity.KeyValue
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentCarFeeBinding
import com.syqc.monitor.ui.newaddcar.KeyValuePopup
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil
import com.syqc.videos.CarMonitorViewModel

class CarFeeFragment : LazyFragment<FragmentCarFeeBinding>(FragmentCarFeeBinding::inflate) {
    private val fragments = mutableListOf<Fragment>()
    private val mFeeTypePopup by lazy { KeyValuePopup(requireContext()) }
    private val mViewModel by activityViewModels<CarFeeViewModel>()
    private var adapter: FragStateAdapter? = null
    private val oilFragment by lazy { OilFragment() }
    private val mActivityVM by activityViewModels<CarMonitorViewModel>()
    override fun initView(view: View) {
        fragments.add(RepairFragment())
        fragments.add(TrafficFragment())
        fragments.add(oilFragment)
        fragments.add(InsureFragment())
        fragments.add(CarYearFragment())
        fragments.add(OtherFragment())
        adapter = FragStateAdapter(this, fragments)
        binding.vpFee.isUserInputEnabled = false
        binding.vpFee.offscreenPageLimit = 6
        binding.vpFee.adapter = adapter
        binding.vpFee.currentItem = 0
        mViewModel.index = 0
        binding.lrvFeeType.setRightText(mViewModel.getFeeTypes()[mViewModel.index].value)
    }

    override fun initData() {
        mActivityVM.carId.observe(this) {
            mViewModel.carId = it
        }
        mActivityVM.carName.observe(this) {
            binding.lrvChooseCar.setRightText(it)
            binding.lrvChooseCar.isEnabled = false
        }
    }

    override fun initEvent() {
        binding.lrvChooseCar.setOnClickListener {
            if (NetDataUtil.isSyncTree) {
                launcher.launchAct<TeamsCarsActivity>(requireContext())
            } else {
                launcher.launchAct<ChooseCarActivity>(requireContext())
            }
        }

        binding.lrvFeeType.setOnClickListener {
            mFeeTypePopup.showAtLocation(
                requireActivity().window.decorView,
                R.string.desc_fee_type,
                mViewModel.getFeeTypes(),
                mViewModel.index
            )
        }

        mFeeTypePopup.setCallBack(object : KeyValuePopup.CallBack {
            override fun onBack(entity: KeyValue, index: Int) {
                mViewModel.index = index
                binding.vpFee.currentItem = index
                binding.lrvFeeType.setRightText(entity.value)
                mViewModel.clearLive.postValue(true)
            }
        })
        binding.vpFee.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position == 2) {
                    binding.vpFee.postDelayed({ oilFragment.clearData() }, 300)
                }
            }
        })
    }

    private val launcher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME)
            binding.lrvChooseCar.setRightText(carName)
            mViewModel.carId = carId
        }
    }
}

class FragActStateAdapter(activity: FragmentActivity, private val fragments: List<Fragment>) :
    FragmentStateAdapter(activity) {
    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

}

class FragStateAdapter(fragment: Fragment, private val fragments: List<Fragment>) :
    FragmentStateAdapter(fragment) {
    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

}